package com.xiaxiang.xiawan.Effect;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.TextView;

import com.xiaxiang.xiawan.Effect.Adapter.EffectAdapter;
import com.xiaxiang.xiawan.Effect.Adapter.EffectData;
import com.xiaxiang.xiawan.R;
import com.xiaxiang.xiawan.Util.XXToolUtil;
import com.xiaxiang.xiaxwan.ar.data.XXARMaterialVideoData;

import java.util.ArrayList;


/**
 * Created by mk on 2017/8/4.
 */

public class EffectView implements GridView.OnItemClickListener{
    private String mEffectViewName;
    private int mEffectViewId = 0;
    private View view;
    private GridView effect_list;
    private Context mContext;
    private EffectAdapter effectAdapter;
    private ArrayList<EffectData> mEffectDataList;
    private ARMediaOperator mARMediaOperator;
    private int mSelectIndex = -1;
    private EffectData mSelectData;
    private EffectData mLastSelectData;
    private TextView tvTitle;
    private TextView tvContent;
    public EffectView(Context context,ARMediaOperator arMediaOperator) {
        mContext = context;
        mARMediaOperator = arMediaOperator;
        view = LayoutInflater.from(context).inflate(R.layout.effect_tab_view, null);
        mEffectDataList = new ArrayList<EffectData>();
        effect_list = view.findViewById(R.id.effect_list);
        effectAdapter = new EffectAdapter(context,mEffectDataList);
        effect_list.setAdapter(effectAdapter);
        effect_list.setOnItemClickListener(this);
    }

    public void initTextView(View mainView){
        tvTitle = (TextView)mainView.findViewById(R.id.tvTitle);
        tvContent = (TextView)mainView.findViewById(R.id.tvContent);
    }
    public  View getView() {
        return view;
    }

    public void addEffectData(EffectData data){
        effectAdapter.addEffectData(data);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(mEffectDataList == null || mEffectDataList.size() == 0){
            return;
        }
        mSelectData = mEffectDataList.get(i);
        if(mSelectData == null){
            return;
        }
        tvTitle.setText(mSelectData.mEffectName);
        tvContent.setText(mSelectData.mEffectName);
        if(mSelectIndex == i){
            //setCheck(view);
        }else{
            if(mSelectData != mLastSelectData){
                mSelectData.mIsSelect = true;
                if(mLastSelectData != null){
                    mLastSelectData.mIsSelect = false;
                    mARMediaOperator.pauseVideoPlayer();
                    mARMediaOperator.onDestroy();
                }
                effectAdapter.notifyDataSetChanged();
            }
            Log.e("ur","select index is "+i+" mSelectData.mEffectMediaUrl is "+mSelectData.mEffectMediaUrl);
           mARMediaOperator.setVideoPath(mSelectData.mEffectMediaUrl);
            mARMediaOperator.startVideoPlayer();
        }
    //    start();
        mSelectIndex = i;
        mLastSelectData = mSelectData;
    }


    private void setCheck(View view){
        CheckBox checkBox = view.findViewById(R.id.ckCheck);
        boolean is = checkBox.isChecked();
        if(!is){
            checkBox.setVisibility(View.VISIBLE);
        }else{
            checkBox.setVisibility(View.GONE);
        }
        checkBox.setChecked(!is);
    }
    public int getSelectIndex(){
        return mSelectIndex;
    }

    public EffectData getSelectEffectData(){
        if(mSelectData == null && mEffectDataList.size() > 0){
            mSelectData = mEffectDataList.get(0);
        }
        return mSelectData;
    }

    public int getEffectDataListSize(){
        return mEffectDataList.size();
    }

    public void simulationOnItemClick()
    {
        effect_list.performItemClick( null, 0, 0);
    }

    private void start(){
        Intent intent = new Intent("com.xiaxiang.xiawan.video");
        intent.setAction("com.xiaxiang.xiawan.video");
        intent.setPackage(mContext.getPackageName());

        XXARMaterialVideoData data = new XXARMaterialVideoData();
        data.mVideoPath = mSelectData.mEffectMediaUrl;
        data.setNeedVideoPath(true);
        data.mMaterialId = mSelectData.mEffectId;
        data.mMaterialUrl = mSelectData.mEffectMediaUrl;
        data.mUserName = "";
        data.mName = mSelectData.mEffectName;
        // data.mMaterialPathName = materialName;
        data.mArMode =  1;
        intent.putExtra(XXToolUtil.AR_MODE_KEY, 1);
        intent.putExtra(XXToolUtil.MATERIAL_VIDEO_DATA_KEY,data);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        ((Activity)mContext).startActivity(intent);
    }
}
